package com.my.controller;

import cn.hutool.core.util.StrUtil;
import com.my.service.UserService;
import com.my.service.impl.UserServiceImpl;
import com.my.vo.UserVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userIdStr = req.getParameter("userId");
        List<UserVo> userList = new ArrayList<UserVo>();
        if(StrUtil.isNotEmpty(userIdStr)){
            Integer userId = Integer.parseInt(userIdStr);
            UserVo userVo = userService.getUserById(userId);
            userList.add(userVo);
        }else {
            userList = userService.getAllUser();
        }
        req.setAttribute("userList",userList);
    }
}
